// shouye/findteacher/findteacher.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // companys:[
    //   {   
    //     imgurl:"",
    //     name:"",
    //     zhongzhi:'',
    //     subjects:'',
    //     isrenzhen:'',
    //     address:''
    // },
    //   {
    //     imgurl: "",
    //     name: "",
    //     zhongzhi: '',
    //     subjects: '',
    //     isrenzhen: '',
    //     address: ''


    //   },
    //   {
    //     imgurl: "",
    //     name: "",
    //     zhongzhi: '',
    //     subjects: '',
    //     isrenzhen: '',
    //     address: ''


    //   }
    //   ],
    companys:[],
    pageNo:2,
    pageSize:10,
    username: 'teacher',
    qingkuang: '热爱教育，热爱生活',
    subjects: '初中物理，初中数学',
    address: '成都市春熙路',
    subDisplay:false,
    subArray:[
     {
       id:'1',
       value:'语文'
     }
    ]
  },  
  changeSubject:function(event){
    var id=event.currentTarget.dataset.id
    var name=event.currentTarget.dataset.name
    var that=this
    wx.request({
      url: app.globalData.baseUrl+'teacher/info/search',
      method:'get',
      data:{
        speciality:name,
        pageNo:1,
        pageSize:40
      },
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        that.setData({
          companys:[]
        })
        console.log(res)
          if(res.data.data==null){

          }else{

          
          var resData = res.data.data.data
          for (var index = 0; index < resData.length; index++) {
            var id = 'companys[' + index + '].id'
            var name = 'companys[' + index + '].name'
            var imgurl = 'companys[' + index + '].imgurl'
            var subject = 'companys[' + index + '].subject'
            var address = 'companys[' + index + '].address'
            var xuanyan = 'companys[' + index + '].xuanyan'
            var sex = 'companys[' + index + '].sex'
            var distance = resData[index].distance
            var danwei
            if (distance > 1000) {
              distance = parseInt(distance / 1000)
              danwei = 'km'
            } else {
              distance = parseInt(distance)
              danwei = 'm'
            }
            var dis = 'companys[' + index + '].distance'
            var dan = 'companys[' + index + '].danwei'
            var motton = resData[index].motto
            if (motton.length > 12) {
              motton = motton.substring(0, 12) + '...'
            }
            var gender = resData[index].user.gender
            var s
            if (gender == '0') {
              s = true
            } else {
              s = false
            }
            that.setData({
              [id]: resData[index].id,
              [name]: resData[index].user.realName,
              [imgurl]: resData[index].user.avatarUrl,
              [subject]: resData[index].speciality,
              [address]: resData[index].user.position,
              [sex]: s,
              [xuanyan]: motton,
              [dis]: distance,
              [dan]: danwei

            })
          }
          }
        
       
      }
    })
     this.setData({
       subDisplay:!this.data.subDisplay
     })
  },
  select:function(event){
    wx.pageScrollTo({
      scrollTop: 0,
    })
    this.setData({
      subDisplay:!this.data.subDisplay
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
  
    wx.request({
      url: app.globalData.baseUrl+'teacher/info',
      method:'GET',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },  
      success:function(res){
        console.log(res)
        var resData=res.data.data.data
      for(var index=0;index<resData.length;index++){
          var id='companys['+index+'].id'
          var name='companys['+index+'].name'
          var imgurl='companys['+index+'].imgurl'
          var subject='companys['+index+'].subject'
          var address='companys['+index+'].address'
          var xuanyan='companys['+index+'].xuanyan'
          var sex='companys['+index+'].sex'
          var distance=resData[index].distance
          var danwei
          if(distance>1000){
            distance=parseInt(distance/1000)
            danwei='km'
          }else{
            distance=parseInt(distance)
            danwei='m'
          }
          var dis='companys['+index+'].distance'
          var dan='companys['+index+'].danwei'
        var motton = resData[index].entityData.motto
        if(motton.length>12){
          motton=motton.substring(0,12)+'...'
        }
        var gender = resData[index].entityData.user.gender
        var s
        if(gender==0){
          s=true
        }else{
          s=false
        }
        var realName1= resData[index].entityData.user.realName
        if(realName1==null){
          realName1 = resData[index].entityData.user.nickName
        }
          that.setData({
            [id]:resData[index].entityData.id,
            [name]:realName1,
            [imgurl]: resData[index].entityData.user.avatarUrl,
            [subject]: resData[index].entityData.speciality,
            [address]: resData[index].entityData.user.position,
            [sex]:s,
            [xuanyan]:motton,
            [dis]:distance,
            [dan]:danwei
           
          })
        }
      }
    })
  }, 
  getMoreTeacherNews:function(page){
    var that = this

    wx.request({
      url: app.globalData.baseUrl + 'teacher/info',
      method: 'GET',
      data:{
         pageNo:page,
         pageSize:10
      },
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        try {
          var resData = res.data.data.data
          for (var index = (page - 1) * 10; index < (page - 1) * 10 + resData.length; index++) {
            var id = 'companys[' + index + '].id'
            var name = 'companys[' + index + '].name'
            var imgurl = 'companys[' + index + '].imgurl'
            var subject = 'companys[' + index + '].subject'
            var address = 'companys[' + index + '].address'
            var xuanyan = 'companys[' + index + '].xuanyan'
            var sex = 'companys[' + index + '].sex'
            var distance = resData[index % 10].distance
            var danwei
            if (distance > 1000) {
              distance = parseInt(distance / 1000)
              danwei = 'km'
            } else {
              distance = parseInt(distance)
              danwei = 'm'
            }
            var dis = 'companys[' + index + '].distance'
            var dan = 'companys[' + index + '].danwei'
            var motton = resData[index % 10].entityData.motto
            if (motton.length > 12) {
              motton = motton.substring(0, 12) + '...'
            }
            var gender = resData[index % 10].entityData.user.gender
            var s
            if (gender == '0') {
              s = true
            } else {
              s = false
            }
            that.setData({
              [id]: resData[index % 10].entityData.id,
              [name]: resData[index % 10].entityData.user.realName,
              [imgurl]: resData[index % 10].entityData.user.avatarUrl,
              [subject]: resData[index % 10].entityData.teacherType.name,
              [address]: resData[index % 10].entityData.user.position,
              [sex]: s,
              [xuanyan]: motton,
              [dis]: distance,
              [dan]: danwei

            })
          }
        } catch (err) {
          console.log(err)
          wx.showToast({
            title: '暂时没有更多数据',
            icon: 'none'
          })
        }
        
      }
    })
  },

  /*
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // for(var i=0;i<100;i++)
    // {
    //   app.qingqiu('user/insert/teacherinfo',{userId:i,shoolId:i+1,typeId:i})
    // }
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'demand/listcourse',
      method: 'get',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        for (var i = 0; i < res.data.data.data.length; i++) {
          var id = 'subArray[' + i + '].id'
          var value = 'subArray[' + i + '].value'
          that.setData({
            [id]: res.data.data.data[i].id,
            [value]: res.data.data.data[i].name
          })
          console.log(that.data.subArray[i].id)
        }
      }
    })
  },
  goteacher:function(event)
  {
    var id=event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../teacher/teacher?id='+id,
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
       var that=this
      wx.getSystemInfo({
        success: function(res) {
          that.setData({
            zheHeight:res.windowHeight
          })
        },
      })
      
      this.setData({
        subDisplay:false
      })
  }, 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   this.getMoreTeacherNews(this.data.page)
   this.setData({
     page:this.data.page+1
   })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})